var app = getApp()
var address = require('../../../utils/city.js')
var utils = require('../../../utils/utils.js')
Page({
  data: {
    addressId: null,
    address: '', //详细收货地址（四级）
    value: [0, 0, 0], // 地址选择器省市区 暂存 currentIndex
    region: '', //所在地区
    regionValue: [0, 0, 0], // 地址选择器省市区 最终 currentIndex
    provinces: [], // 一级地址
    citys: [], // 二级地址
    areas: [], // 三级地址
    visible: false,
    defaultSize: 'default',
    primarySize: 'default',
    warnSize: 'default',
    disabled: false,
    plain: false,
    loading: false
  },
  onLoad(options) {
    // 默认联动显示北京
    var id = address.provinces[0].id 
    this.setData({
      addressId :options.id,
      provinces: address.provinces, // 34省
      citys: address.citys[id], //默认北京市辖区
      areas: address.areas[address.citys[id][0].id]
    })
    //console.log("add-address-addressId",this.data.addressId)
    var that = this
    var param= {"id":this.data.addressId};
    wx.request({
      url: utils.sysConst.getAddressByIdPath,
      method: 'POST',
      header: {
        "Content-Type": "application/x-www-form-urlencoded",'appVersion':utils.appVersion,'signValue':utils.getSignValue(param)
      },
      data: param,
      dataType: "json",
      success: function(res) {
        if(res.data.code == 1){
          var checkFlag = false;
          if ( res.data.data.defaultFlag == 1){
            checkFlag = true;
          }
          that.setData({
            region:  res.data.data.address,
            homeAddress: res.data.data.homeAddress,
            addressee: res.data.data.addressee,
            mobile: res.data.data.mobile,
            switchDefaultFlagChecked: checkFlag,
          })
        }
      }
    })
    //
  },
  closePopUp() {
    this.setData({
      visible: false
    })
  },
  pickAddress() {
    this.setData({
      visible: true,
      value: [...this.data.regionValue]
    })
  },
  // 处理省市县联动逻辑 并保存 value
  cityChange(e) {
    var value = e.detail.value
    let {
      provinces,
      citys
    } = this.data
    var provinceNum = value[0]
    var cityNum = value[1]
    var areaNum = value[2]

    if (this.data.value[0] !== provinceNum) {
      var id = provinces[provinceNum].id
      this.setData({
        value: [provinceNum, 0, 0],
        citys: address.citys[id],
        areas: address.areas[address.citys[id][0].id]
      })
    } else if (this.data.value[1] !== cityNum) {
      var id = citys[cityNum].id
      this.setData({
        value: [provinceNum, cityNum, 0],
        areas: address.areas[citys[cityNum].id]
      })
    } else {
      this.setData({
        value: [provinceNum, cityNum, areaNum]
      })
    }
  },
  preventTouchmove() {},
  // 城市选择器
  // 点击地区选择取消按钮
  cityCancel(e) {
    this.setData({
      citys: this.data.lastCitys,
      areas: this.data.lastAreas,
      value: [...this.data.regionValue],
      visible: false
    })
  },
  // 提交时由序号获取省市区id
  getRegionId(type) {
    let value = this.data.regionValue
    let provinceId = address.provinces[value[0]].id
    let townId = address.citys[provinceId][value[1]].id
    let areaId = ''
    if (address.areas[townId][value[2]].id) {
      areaId = address.areas[townId][value[2]].id
    } else {
      areaId = 0
    }

    if (type === 'provinceId') {
      return provinceId
    } else if (type === 'townId') {
      return townId
    } else {
      return areaId
    }
  },
  // 点击地区选择确定按钮
  citySure(e) {
    var value = this.data.value
    this.setData({
      visible: false
    })
    // 将选择的城市信息显示到输入框
    try {
      var region = (this.data.provinces[value[0]].name || '') + (this.data.citys[value[1]].name || '')
      if (this.data.areas.length > 0) {
        region = region + this.data.areas[value[2]].name || ''
      } else {
        this.data.value[2] = 0
      }

    } catch (error) {
      console.log('adress select something error')
    }

    this.setData({
      region: region,
      lastCitys: this.data.citys,
      lastAreas: this.data.areas,
      regionValue: [...this.data.value]
    }, () => {
      //console.log(`region ${this.getRegionId('region')} 省份ID：${this.getRegionId('provinceId')}: 市区ID：${this.getRegionId('townId')}：城区ID：${this.getRegionId('areas')}`)
    })
  },

  editAddress:function (event) {
    var defaultFlag ="0";
    if (event.detail.value.defaultFlag){
      defaultFlag ="1";
    }
    var provinceCode =this.getRegionId('provinceId');
    var cityCode =this.getRegionId('townId');
    var areaCode =this.getRegionId('areas');
    //console.log(`省份ID：${cityCode}: 市区ID：${cityCode}：城区ID：${areaCode}`)
    var homeAddress =event.detail.value.homeAddress;
    var addressee =event.detail.value.addressee;
    var mobile =event.detail.value.mobile
    if (homeAddress == null || homeAddress ==""){
      wx.showToast({
        title:"请输入门牌号",
        icon: 'none',
        duration: 1000
      })
      return false;
    }if (addressee == null || addressee ==""){
      wx.showToast({
        title:"请输入收货人姓名",
        icon: 'none',
        duration: 1000
      })
      return false;
    }
    if (mobile == null || mobile ==""){
      wx.showToast({
        title:"请输入收货人手机",
        icon: 'none',
        duration: 1000
      })
      return false;
    }

    this.setData({
      disabled: !this.data.disabled,
      loading: !this.data.loading
    })
    wx.showLoading({
      title: '处理中...',
    })
    var param = {"id":this.data.addressId
      ,"provinceCode":provinceCode
      ,"cityCode":cityCode
      ,"areaCode":areaCode
      ,"homeAddress":homeAddress
      ,"addressee":addressee
      ,"mobile":mobile
      ,"defaultFlag":defaultFlag
    }
    wx.request({
      url: utils.sysConst.editAddressPath,
      method: 'POST',
      header: {
        "Content-Type": "application/x-www-form-urlencoded",'appVersion':utils.appVersion,'signValue':utils.getSignValue(param)
      },
      data:param ,
      dataType: "json",
      success: function(res) {
        wx.hideLoading();
        // console.log(res)
        if (res.data.code == 1){
          wx.navigateBack({
            delta: 1
          })
          /*wx.navigateTo({
            url: '/pages/personal/userAddress/userAddress?type=editBack',
          })*/
        }else{
          wx.showToast({
            title:res.data.msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
    //
    this.setData({
      disabled: !this.data.disabled,
      loading: !this.data.loading
    })
    //
  },

})